package com.hmyd.hmyd_android.ui;

import java.lang.reflect.Type;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.BillDetailDto;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 个人票据个人投资记录详情
 * @author wangk
 *
 */


@ContentView(R.layout.activity_bill_type_detail)
public class BillTypeDetailActivity extends BasicActivity {
	
	private final static String ITEMRECORDID = "itemRecordId";
	
	private BillDetailDto billDetailDto;
	
	private final static int BILL_REPAYMENT_RECORD_DETAIL = 1;
	
	@ViewInject(R.id.layout_home_title)
	private View layout_home_title;
	@ViewInject(R.id.home_title_txt)
	private TextView home_title;
	@ViewInject(R.id.home_right)
	private TextView home_right;
	@ViewInject(R.id.real_income)
	private TextView real_income;
	@ViewInject(R.id.total_expect_income)
	private TextView total_expect_income;
	@ViewInject(R.id.bill_detail_chart)
	private LinearLayout bill_detail_chart; // 表格填充
	
	public static Intent getIntent(Context context,String billId) {
		Intent intent = new Intent(context,BillTypeDetailActivity.class);
		intent.putExtra(ITEMRECORDID, billId);
		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onPostSuccess(int postId, String msg) {
		Gson gson = new Gson();
		switch (postId) {
		case BILL_REPAYMENT_RECORD_DETAIL:
			Type type = new TypeToken<BillDetailDto>() {
			}.getType();
			billDetailDto = gson.fromJson(msg, type);
			Log.e("data bill", msg);
			if(billDetailDto != null) {
				// 明日收益
				View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_bill_detail_chart_item, null);
				TextView introducation = (TextView) view.findViewById(R.id.introducation); // 说明
				TextView principal = (TextView) view.findViewById(R.id.principal); // 本金
				TextView expect_earning = (TextView) view.findViewById(R.id.expect_earning); // 预期收益
				TextView platform_management_fee = (TextView) view.findViewById(R.id.platform_management_fee); // 平台管理费
				TextView real_earning = (TextView) view.findViewById(R.id.real_earning); // 实际收益
				
				// 宽度适配
				introducation.setWidth(findViewById(R.id.introducation_label).getWidth());
				principal.setWidth(findViewById(R.id.principal_label).getWidth());
				expect_earning.setWidth(findViewById(R.id.expect_earning_label).getWidth());
				platform_management_fee.setWidth(findViewById(R.id.platform_management_fee_label).getWidth());
				real_earning.setWidth(findViewById(R.id.real_earning_labe).getWidth());
				
				principal.setText("——");
				introducation.setText("预期明日收益");
				
				expect_earning.setText(billDetailDto.expectIncome+"");
				platform_management_fee.setText(billDetailDto.userFee+"");
				real_earning.setText(billDetailDto.realIncome+"");
				bill_detail_chart.addView(view);
				// 累积收益
				View totalView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.view_bill_detail_chart_item, null);
				TextView totalIntroducation = (TextView) totalView.findViewById(R.id.introducation); // 说明
				TextView totalPrincipal = (TextView) totalView.findViewById(R.id.principal); // 本金
				TextView totalExpect_earning = (TextView) totalView.findViewById(R.id.expect_earning); // 累积预期收益
				TextView totalPlatform_management_fee = (TextView) totalView.findViewById(R.id.platform_management_fee); // 累积平台管理费
				TextView totalReal_earning = (TextView) totalView.findViewById(R.id.real_earning); // 累积实际收益
				// 宽度适配
				totalIntroducation.setWidth(findViewById(R.id.introducation_label).getWidth());
				totalPrincipal.setWidth(findViewById(R.id.principal_label).getWidth());
				totalExpect_earning.setWidth(findViewById(R.id.expect_earning_label).getWidth());
				totalPlatform_management_fee.setWidth(findViewById(R.id.platform_management_fee_label).getWidth());
				totalReal_earning.setWidth(findViewById(R.id.real_earning_labe).getWidth());
				
				
				totalIntroducation.setText("项目累计");
				totalPrincipal.setText(billDetailDto.principal+"");
				totalExpect_earning.setText(billDetailDto.sumExpectIncome+"");
			
				totalPlatform_management_fee.setText(billDetailDto.sumUserFee+"");
				totalReal_earning.setText(billDetailDto.sumRealIncome+"");
				
				bill_detail_chart.addView(totalView);
				
				total_expect_income.setText(billDetailDto.totalExpectIncome+"");
				real_income.setText(billDetailDto.totalRealIncome+"");
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		switch (postId) {
		case BILL_REPAYMENT_RECORD_DETAIL:
			MyToast.showHMToast(BillTypeDetailActivity.this, false, "获取详情失败,错误信息"+msg);
			break;

		default:
			break;
		}
	}

	@Override
	protected void setParams() {
		String itemRecord = this.getIntent().getStringExtra(ITEMRECORDID);
		layout_home_title.setOnTouchListener(touch);
		home_right.setText("合同下载");
		home_title.setText("投资记录详情");
		RequestParams params = new RequestParams();
		params.addBodyParameter(ITEMRECORDID, itemRecord);
		onRequest(BILL_REPAYMENT_RECORD_DETAIL, UrlConstant.BILL_REPAYMENT_DETAIL, HttpMethod.GET,
				params);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		if(billDetailDto != null && !TextUtils.isEmpty(billDetailDto.contractUrl)) {
			Utils.jumpExplorer(BillTypeDetailActivity.this, billDetailDto.contractUrl);
		}
	}
	
}
